package com.movitech.mobile.cz.base.purchase.bizcontroller;


import com.movitech.mobile.cz.base.common.util.PageCode;
import com.movitech.mobile.cz.base.common.util.excel.ExportExcel;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.purchase.bizvo.SpecialPurchasePlanListResponseVO;
import com.movitech.mobile.cz.base.purchase.entity.ext.*;
import com.movitech.mobile.cz.base.purchase.service.PContactPlanService;
import com.movitech.mobile.cz.base.purchase.service.PSpecialPurchasePlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 合约和专线采购计划 前端控制器
 * </p>
 *
 * @author Blue.Xia
 * @since 2018-03-08
 */
@RestController
@RequestMapping("/v1/contractSpecialPurchasePlan")
@Api(value = "合约和专线采购计划操作接口", description = "合约和专线采购计划操作接口")
public class ContractSpecialPurchasePlanController {

    @Autowired
    private PContactPlanService pContactPlanService;

    @Autowired
    private PSpecialPurchasePlanService pSpecialPurchasePlanService;

    @PostMapping("/findContractPlanList")
    @ApiOperation(value = "合约规划列表查询接口", notes = "合约规划列表查询接口")
    public RestReturnResult findContractPlanList(@RequestBody PContactPlanFindRequest pContactPlanFindRequest) {
        RestReturnResult restResult = RestReturnResult.success();
        
        try{
        	PageCode page = pContactPlanService.findContractPlanList(pContactPlanFindRequest);
            restResult.setData(page);
        }catch(Exception e){
        	e.printStackTrace();
        	restResult = RestReturnResult.error();
        }
        
        return restResult;
    }


    @GetMapping("/getPContractPlanById/{contractPlanId}")
    @ApiOperation(value = "根据合约规划ID查询接口", notes = "根据合约规划ID查询接口")
    public RestReturnResult getPContractPlanById(@PathVariable String contractPlanId) {
        RestReturnResult restResult = RestReturnResult.success();
        ExtPContactPlan extPContactPlan = pContactPlanService.getPContractPlanById(contractPlanId);

        restResult.setData(extPContactPlan);
        return restResult;
    }

    @PostMapping("/getPContractPlanByIds")
    @ApiOperation(value = "合并生成专项采购计划查询接口", notes = "合并生成专项采购计划查询接口")
    public RestReturnResult getPContractPlanByIds(@RequestBody PContactPlanGetRequest pContactPlanGetRequest) {

        RestReturnResult restReturnResult = pContactPlanService.getPContractPlanByIds(pContactPlanGetRequest);
        return restReturnResult;
    }

    @PostMapping("/findSpecialPurchasePlanList")
    @ApiOperation(value = "专项采购计划列表查询接口", notes = "专线采购计划列表查询接口")
    public RestReturnResult findSpecialPurchasePlanList(@RequestBody PSpecialPurchasePlanFindRequest pSpecialPurchasePlan) {
        RestReturnResult restResult = RestReturnResult.success();
        
        try{
        	PageCode page = pSpecialPurchasePlanService.findSpecialPurchasePlanList(pSpecialPurchasePlan);
            restResult.setData(page);
        }catch(Exception e){
        	e.printStackTrace();
        	restResult = RestReturnResult.error();
        }
        
        return restResult;
    }

    @PostMapping("/exportPlans")
    @ApiOperation(value = "采购计划导出", notes = "采购计划导出")
    public void exportPlans(@RequestBody  PSpecialPurchasePlanFindRequest pSpecialPurchasePlan,
                            HttpServletResponse response, HttpServletRequest request) {
        try {
            //PSpecialPurchasePlanFindRequest pSpecialPurchasePlan = new PSpecialPurchasePlanFindRequest();
            String fileName = "专项采购计划.xlsx";
            new ExportExcel("专项采购计划", SpecialPurchasePlanListResponseVO.class).setDataList(pSpecialPurchasePlanService.findSpecialPurchasePlanListNoPage(pSpecialPurchasePlan))
                    .write(request,response, fileName).dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/getSpecialPurchasePlanById/{SpecialPurchasePlanId}")
    @ApiOperation(value = "专项采购计划根据ID查询接口", notes = "专项采购计划根据ID查询接口")
    public RestReturnResult getSpecialPurchasePlanById(@PathVariable String SpecialPurchasePlanId) {
        RestReturnResult restResult = RestReturnResult.success();
        ExtPSpecialPurchasePlan pSpecialPurchasePlan = pSpecialPurchasePlanService.getSpecialPurchasePlanById(SpecialPurchasePlanId);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("data",pSpecialPurchasePlan);
        restResult.setData(map);
        return restResult;
    }

    @PostMapping("/saveSpecialPurchasePlan")
    @ApiOperation(value = "保存专项采购计划接口", notes = "保存专项采购计划接口")
    public RestReturnResult saveSpecialPurchasePlan(@RequestBody PSpecialPurchasePlanSaveRequestVO pSpecialPurchasePlanSaveRequestVO) {
        RestReturnResult restResult = RestReturnResult.success();
        
        restResult = pSpecialPurchasePlanService.saveSpecialPurchasePlan(pSpecialPurchasePlanSaveRequestVO);
        
        return restResult;
    }

    @GetMapping("/deleteSpecialPurchasePlanById/{specialPurchasePlanId}")
    @ApiOperation(value = "专项采购计划通过ID删除接口", notes = "专项采购计划通过ID删除接口")
    public RestReturnResult deleteSpecialPurchasePlanById(@PathVariable String specialPurchasePlanId) {
        RestReturnResult restResult = RestReturnResult.success();
        pSpecialPurchasePlanService.deleteById(specialPurchasePlanId);
        return restResult;
    }

    @GetMapping("/getConfirmedProjects/{regionalCompanyId}")
    @ApiOperation(value = "采购计划确认项目列表", notes = "专项采购计划确认")
    public RestReturnResult getConfirmedProjects(@PathVariable String regionalCompanyId){
    	RestReturnResult restResult = RestReturnResult.success();
    	restResult = pSpecialPurchasePlanService.getConfirmedProjects(regionalCompanyId);
    	
    	return restResult;
    }

}

